<?php
/**
 * @title 167. 两数之和 II - 输入有序数组 - 滑动窗口
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $numbers
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($numbers, $target) {
        /**
         * @since 2020-07-20 双指针
         */
        $left = 0;
        $right = count($numbers)-1;

        /**
         * @since 2020-07-20 滑动窗口, 动态规划
         */
        while($left<$right){
            $sum = $numbers[$left]+$numbers[$right];

            if($sum == $target){
                break;
            } elseif($sum>$target) {
                $right--;
            } else {
                $left++;
            }
        }

        /**
         * @return
         */
        return [$left+1, $right+1];
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.20.php
 */
$datas = [
    [
        [2, 7, 11, 15], 9
    ]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->twoSum($data[0], $data[1]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
